﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace VipSys.Common.Library
{
    public class DBAccess
    {
        private OleDbConnection m_sqlConnection ;
        private OleDbCommand m_SqlCommand;
        private OleDbDataAdapter m_SqlDataAdapter;
        private string m_connString = string.Empty;
        /// <summary>
        /// 获取或设置连接字符串
        /// </summary>
        public string ConnectionString
        {
            get { return this.m_connString; }
            set { this.m_connString = value; }
        }

        private void OpenConnection() {
            try
            {
                this.m_sqlConnection = new OleDbConnection(m_connString);
                this.m_sqlConnection.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void CloseConnection()
        {
            if (this.m_sqlConnection == null)
                return;
            this.m_sqlConnection.Close();
        }

        public int ExecuteNonQuery(string sql)
        {
            int count = 0;
            try
            {
                this.OpenConnection();
                m_SqlCommand = new OleDbCommand(sql, this.m_sqlConnection);
                count = m_SqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CloseConnection();
            }
            return count;
        }

        //返回DataSet
        public DataSet ExecuteQuery(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                this.OpenConnection();
                m_SqlDataAdapter = new OleDbDataAdapter(sql, this.m_sqlConnection);
                this.m_SqlDataAdapter.Fill(ds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CloseConnection();
            }
            return ds;
        } 

    }
}
